﻿using LightWadf.Result;
using LightWadf.Util;
using System;
using System.IO;
using System.Web;

namespace LightWadf.Analyzer
{
    public class DefaultResultAnalyzer : IResultAnalyzer
    {
        public void Parse(object result, HttpContext context)
        {
            if (result != null)
            {
                if (result is JsonResult)
                {
                    context.Response.ContentType = "application/json";
                    context.Response.Write((result as JsonResult).JsonString);

                    LogUtility.Trace(string.Format("{0} 的响应结果为 json，已成功返回", context.Request.Path));
                }
                else if (result is RedirectResult)
                {
                    var newUrl = (result as RedirectResult).Url;
                    context.Response.Redirect(newUrl);

                    LogUtility.Trace(string.Format("{0} 已重定向至 {1}", context.Request.Path, newUrl));
                }
                else if (result is HtmlResult)
                {
                    context.Response.Write((result as HtmlResult).HtmlString);

                    LogUtility.Trace(string.Format("{0} 的响应结果为 Html，已成功返回", context.Request.Path));
                }
                else if (result is RazorResult)
                {
                    context.Response.Write((result as RazorResult).HtmlString);

                    LogUtility.Trace(string.Format("{0} 的响应结果为 Razor 页面，已成功返回", context.Request.Path));
                }
                else if (result is AspxResult)
                {
                    using (TextWriter tw = new StringWriter())
                    {
                        context.Server.Execute((result as AspxResult).Path, tw, true);
                        context.Response.Write(tw.ToString());
                    }

                    LogUtility.Trace(string.Format("{0} 的响应结果为 ASP 页面，已成功返回", context.Request.Path));
                }
                else if (result is FileResult)
                {
                    FileInfo file = new FileInfo((result as FileResult).FilePath);
                    DateTime lastModified = File.GetLastWriteTimeUtc(file.FullName);
                    DateTime time = Convert.ToDateTime(context.Request.Headers["If-Modified-Since"]);
                    // lastModified 精确到毫秒，time 只精确到秒，会有 1 秒以内的误差
                    if ((lastModified - time).TotalMilliseconds < 1000.0)
                    {
                        context.Response.StatusCode = 304;
                        LogUtility.Trace(string.Format("{0} 的响应结果为静态文件，且未过期，已返回 304", context.Request.Path));
                    }
                    else
                    {
                        context.Response.AddHeader("CacheControl", "max-age=" + int.MaxValue);
                        context.Response.AddHeader("Last-Modified", file.LastWriteTimeUtc.ToString());
                        string suf = Utility.GetImageSuffix(file.Name);
                        if (!string.IsNullOrEmpty(suf))
                        {
                            context.Response.ContentType = "image/" + suf;
                        }
                        context.Response.WriteFile((result as FileResult).FilePath);
                        LogUtility.Trace(string.Format("{0} 的响应结果为文件，已成功返回", context.Request.Path));
                    }
                }
                else if (result is XmlResult)
                {
                    context.Response.ContentType = "application/xml";
                    context.Response.Write((result as XmlResult).XmlString);
                    LogUtility.Trace(string.Format("{0} 的响应结果为 xml，已成功返回", context.Request.Path));
                }
                else context.Response.Write(result);
            }
            else
            {
                LogUtility.Trace(string.Format("{0} 的响应结果为空", context.Request.Path));
                return;
            }
        }
    }
}